//index.js
//获取应用实例
const app = getApp()
const service = require('../../utils/service.js')
const api = require('../../utils/util.js')

Page({
  data: {
    userInfo: {}, // 用户信息
    shopInfo: {}, // 店铺信息
    navIndex: 1,
    imgUrls: [], // banner列表
    couponList: [], // 优惠券列表
    hotGoodsList: [], // 热门商品
    goodsList: [], // 首页普通商品
    groupList: [], // 拼团列表
    yzcList: [], // yzc商品列表
    page: 1,
    showList: [], // 展示列表
    hasMoreData: true,
    out_user_id: '', // 传进来的out_user_id
    showCoupon: false,
    classfiyList: [],
    showClassfiy: false,
    top:0,
    offon:true
  },
  onLoad: function(options) {
    api.ajax(service.url('getConstShopUser'), {}, res=> {
     
      wx.setStorageSync('out_user_id', res.data.shop_user_id)
      this.shopGetUserShopInfo(res.data.shop_user_id);
      if (options) {
        if (options.share_user_id) {
          wx.setStorageSync('share_user_id', options.share_user_id)
        } else if (wx.getStorageSync('share_user_id')) {
          wx.setStorageSync('share_user_id', wx.getStorageSync('share_user_id'))
        } else {
          wx.setStorageSync('share_user_id', '')
        }
      }
      this.init()
    })
    
  },
  shopGetUserShopInfo: function (out_user_id){
    api.ajax(service.url('getUserShopInfo'), {
      userid: out_user_id
    }, function (res) {
     
      wx.setStorageSync('shop_data_shop', res.data);
    })
  },
  setShareInfo: function() {
    let that = this
    let share_id = wx.getStorageSync('share_user_id')
    api.ajax(service.url('setShareUser'), {
      share_user_id: share_id,
      current_user_id: that.data.userInfo.customer_id
    }, function(res) {
      console.log(res)
    })
  },

  init: function() {
    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
      out_user_id: wx.getStorageSync('out_user_id')
    })
    this.doAjax()
    if (wx.getStorageSync('share_user_id')) {
      this.setShareInfo()
    }
  },
  scroll:function(e){
    this.setData({
      top: e.detail.scrollTop
    })
  },
  doAjax: function() {
    let that = this
    that.getShopInfo(function() {
      that.getShop_uid(function() {
        // 获取banner列表
        that.getBannerList()

        // 获取优惠券列表
        that.getCouponList()

        // 获取店铺所有分类
        that.getAllClassfiy()

        // 获取热门商品
        that.getHotGoods()

        // 获取首页普通商品
        that.getGoods()

        // 获取拼团商品
        that.getGroupList()
      })
      // 获取yzc商品
      // that.getYzcList()
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    })
  },
  // 获取店铺的unid
  getShop_uid: function(cb) {
    console.log('-------------------------------')
    console.log(wx.getStorageSync('userInfo'))
    api.ajax(service.url('getShop_enter'), {
      userid: wx.getStorageSync('userInfo').id
      },
      function(res) {
        if (res.code == 0) {
          
          wx.setStorageSync("shop_data", res.data)
          cb()
        } else {
          api.toast(res.message)
        }
      })
  },
  // 获取店铺信息
  getShopInfo: function(cb) {
    let that = this
    let url = service.url('getShopDetail')
    let out_user_id = that.data.out_user_id
    if (this.data.out_user_id) {
      out_user_id = this.data.out_user_id
    }
    api.ajax(url, {
      userid: out_user_id
    }, function(res) {
      console.log(res);
      if (res.code == 0) {
        that.setData({
          shopInfo: res.data,
        })
        if (!res.data.id){
          that.setData({
            offon: false,
          })
        }
        wx.setStorage({
          key: 'shopInfo',
          data: res.data
        })
        cb()
      } else {
        that.setData({
          offon: false,
        })
        api.toast(res.message)
      }
    })
  },

  // 获取banner列表
  getBannerList: function() {
    let that = this;
    let shop_token = wx.getStorageSync('shop_data_shop').token;
    let shop_token_key = wx.getStorageSync('shop_data').token_key;

    api.ajax(service.url('shopbanner'), {
        unionid: this.data.shopInfo.unionid,
        token: shop_token,
        token_key: shop_token_key,
        shop_code: this.data.shopInfo.shop_code,
        position: 0
      },
      function(res) {
        console.log(res.data)
        if (res.code == 0) {
          that.setData({
            imgUrls: res.data
          })
        } else {
          api.toast(res.message)
        }
      })
  },

  // 获取优惠券列表
  getCouponList: function() {
    let that = this
    let shop_token = wx.getStorageSync('shop_data_shop').token;
    let shop_token_key = wx.getStorageSync('shop_data').token_key;

    api.ajax(service.url('getCouponActiveList'), {
      unionid: this.data.shopInfo.unionid,
      token: shop_token,
      token_key: shop_token_key,
      page: 1,
      is_active: 1,
      user_unionid: that.data.userInfo.shop_unionid
    }, function(res) {
      if (res.code == 0) {
        let isBol = false;
        if (res.data.length > 0) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].is_get != 1) {
              isBol = true;
              break;
            }
          }
        }
        that.setData({
          couponList: res.data,
          showCoupon: isBol
        })
      } else {
        api.toast(res.message)
      }
    })
  },

  // 获取所有分类
  getAllClassfiy: function() {
    let that = this
    api.ajax(service.url('categoryGetAll'), {
      unionid: wx.getStorageSync('shop_data_shop').shop_unionid,
      token: wx.getStorageSync('shop_data').token,
      token_key: wx.getStorageSync('shop_data').token_key,
      shop_code: that.data.shopInfo.shop_code
    }, function(res) {
      if (res.code == 0) {
        that.setData({
          classfiyList: res.data
        })
      } else {
        api.toast(res.message)
      }
    })
  },

  // 领取优惠券
  getCoupon: function(e) {
    let that = this
    let id = e.currentTarget.dataset.id
    let idx = e.currentTarget.dataset.key
    api.ajax(service.url('getCouponDiscount'), {
      coupon_discount_id: id
    }, function(res) {
      if (res.code == 0) {
        that.data.couponList[idx].is_get = 1
        that.setData({
          couponList: that.data.couponList
        })
        api.toast('领取成功')
      } else {
        api.toast(res.message)
      }
    })
  },

  // 获取热门商品
  getHotGoods: function() {
    let that = this;

    api.ajax(service.url('goodsSearch'), {
      unionid: this.data.shopInfo.unionid,
      shop_code: that.data.shopInfo.shop_code,
      hot: 1,
      page: that.data.page,
      is_home: 1
    }, function(res) {
      if (res.code == 0) {
        that.setData({
          hotGoodsList: res.data
        })
      } else {
        api.toast(res.message)
      }
    })
  },
  // 前往个人中心
  toUser:function(){
    wx.navigateTo({
      url: '/shop/me/me',
    })
  },
  // 前往购物车
  goGwc: function () {
    wx.navigateTo({
      url: '/shop/cart/cart',
    })
  },
  // 获取首页普通商品
  getGoods: function() {
    let that = this;

    api.ajax(service.url('goodsSearch'), {
      unionid: this.data.shopInfo.unionid,
      shop_code: that.data.shopInfo.shop_code,
      page: that.data.page
    }, function(res) {
      if (res.code == 0) {
        if (res.data.length > 0) {
          for (var i = 0, len = res.data.length; i < len; i++) {
            that.data.goodsList.push(res.data[i])
          }
          that.setData({
            goodsList: that.data.goodsList,
            showList: that.data.goodsList,
            hasMoreData: true
          })
        } else {
          that.setData({
            hasMoreData: false
          })
        }
      } else {
        api.toast(res.message)
      }
    })
  },

  // 获取拼团商品
  getGroupList: function() {
    let that = this;
    let shop_token = wx.getStorageSync('shop_data').token;
    let shop_token_key = wx.getStorageSync('shop_data').token_key;

    api.ajax(service.url('groupList'), {
      unionid: this.data.shopInfo.unionid,
      token: shop_token,
      token_key: shop_token_key,
      shop_code: that.data.shopInfo.shop_code,
      page: that.data.page,
      is_home: 0
    }, function(res) {
      if (res.code == 0) {
        if (res.data.length > 0) {
          for (var i = 0, len = res.data.length; i < len; i++) {
            that.data.groupList.push(res.data[i])
          }
          that.setData({
            groupList: that.data.groupList,
            hasMoreData: true
          })
        } else {
          that.setData({
            hasMoreData: false
          })
        }
      } else {
        api.toast(res.message)
      }
    })
  },

  // 获取yzc商品
  getYzcList: function() {
    let that = this;
    let shop_token = wx.getStorageSync('shop_data').token;
    let shop_token_key = wx.getStorageSync('shop_data').token_key;

    api.ajax(service.url('getYzcShopGoodsList'), {
      unionid: this.data.shopInfo.unionid,
      token: shop_token,
      token_key: shop_token_key,
      shop_code: that.data.shopInfo.shop_code,
      is_home: 0,
      page: that.data.page
    }, function(res) {
      if (res.code == 0) {
        if (res.data.length > 0) {
          for (var i = 0, len = res.data.length; i < len; i++) {
            that.data.yzcList.push(res.data[i])
          }
          that.setData({
            yzcList: that.data.yzcList,
            hasMoreData: true
          })
        } else {
          that.setData({
            hasMoreData: false
          })
        }
      } else {
        api.toast(res.message)
      }
    })
  },

  // 切换导航
  swtichNav: function(e) {
    let that = this
    let idx = e.target.dataset.type
    this.setData({
      navIndex: idx,
      page: 1,
      hasMoreData: true
    })
    if (idx == 1) {
      this.setData({
        showList: that.data.goodsList
      })
    } else if (idx == 2) {
      this.setData({
        showList: that.data.groupList
      })
    } else if (idx == 3) {
      this.setData({
        showList: that.data.yzcList
      })
    }
  },

  // 前往商品详情
  toGoodsDetail: function(e) {
    var goods_id = e.currentTarget.dataset.id;
    var link_type = e.currentTarget.dataset.linkType;
    var link_type_id = e.currentTarget.dataset.linkTypeId;
    if (!goods_id) {
      return
    }

    var yzc = e.currentTarget.dataset.yzc
    var audit = e.currentTarget.dataset.audit

    let url = `/shop/goods_detail/goods_detail?goods_id=${goods_id}`
    if (yzc && audit == '1') {
      url = `/shop/goods_detail/goods_detail?goods_id=${goods_id}&is_yzc=${yzc}`
    }
    if (link_type == 2) {
      url = `/shop/goods_list/goods_list?category_id=${link_type_id}`
    }
    wx.navigateTo({
      url: url,
    })
  },

  // 前往搜索
  toSearch: function() {
    wx.navigateTo({
      url: '/shop/search/search',
    })
  },

  // 前往分类商品列表
  toClassfiy: function(e) {
    let id = e.currentTarget.dataset.id
    this.setData({
      showClassfiy: false
    })
    wx.navigateTo({
      url: `/shop/goods_list/goods_list?category_id=${id}`,
    })
  },

  // 显示、隐藏分类列表
  toggleClassfiy: function() {
    this.setData({
      showClassfiy: !this.data.showClassfiy
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let type = this.data.navIndex
    let page = this.data.page += 1
    this.setData({
      page: page
    })
    if (this.data.hasMoreData) {
      // do something...
      if (type == 1) {
        this.getGoods()
      } else if (type == 2) {
        this.getGroupList()
      } else if (type == 3) {
        this.getYzcList()
      }
    } else {
      api.toast('没有更多数据了')
    }
  },
  onShareAppMessage: function() {
    let that = this
    let out_id = that.data.userInfo.out_user_id
    return {
      title: '进我的店铺逛逛吧',
      path: `pages/auth/auth?webUrl=shop_index&zf_openid=${that.data.userInfo.openid}&share_user_id=${that.data.userInfo.id}`
    }
  },
  char: function() {
    wx.showModal({
      title: '提示',
      content: '下载APP更方便联系客服',
      confirmText: '下载',
      success: (res) => {
        if (res.confirm) {
          wx.navigateTo({
            url: '/download_app/download_app',
          })
        }
      }
    })
  },

  // 下拉刷新
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading()
    this.setData({
      navIndex: 1,
      page: 1,
      hotGoodsList: [], // 热门商品
      goodsList: [], // 首页普通商品
      groupList: [], // 拼团列表
      yzcList: [], // yzc商品列表
      showList: []
    })
    setTimeout(() => {
      this.init()
    }, 1500)
  },

  closeCouponMask: function() {
    this.setData({
      showCoupon: false
    })
  }
})